package utils

import (
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/mem"
)

type StatusDisk struct {
	UsedMb      int64  `json:"UsedMb"`
	DiskName    string `json:"DiskName"`
	TotalMb     int64  `json:"TotalMb"`
	Available   int64  `json:"AvailableMb"`
	UsedPercent int64  `json:"UsedPercent"`
}
type StatusMen struct {
	UsedMb      int64 `json:"UsedMb"`
	TotalMb     int64 `json:"TotalMb"`
	Available   int64 `json:"AvailableMb"`
	UsedPercent int64 `json:"UsedPercent"`
}

//获取磁盘使用情况
func GetStatusDisk() (jsons interface{}) {
	infos, _ := disk.Partitions(false)
	//创建分区数组
	disks := make([]StatusDisk, len(infos))
	//遍历
	for index, info := range infos {
		// fmt.Println(info.Device)
		if mapUsed, erros := disk.Usage(info.Device); erros != nil {
		} else {
			disks[index].DiskName = info.Device
			disks[index].TotalMb = int64(mapUsed.Total / 1024 / 1024)
			disks[index].UsedMb = int64(mapUsed.Used / 1024 / 1024)
			disks[index].UsedPercent = int64(mapUsed.UsedPercent)
			disks[index].Available = int64(mapUsed.Free / 1024 / 1024)
		}
	}
	// data, _ := json.MarshalIndent(disks, "", "")
	// fmt.Printf()
	// return string(data)
	return disks
}

//获取内容使用情况
func GetStatusMem() (jsons interface{}) {
	var c StatusMen
	v, _ := mem.VirtualMemory()
	c.UsedMb = int64(v.Used / 1024 / 1024)
	c.TotalMb = int64(v.Total / 1024 / 1024)
	c.Available = int64(v.Available / 1024 / 1024)
	c.UsedPercent = int64(v.UsedPercent)
	// data, _ := json.MarshalIndent(c, "", "")
	return c
}
